package com.winning.vsts.merge;

import com.alibaba.fastjson.JSONObject;
import com.winning.vsts.merge.service.configuration.VstsContext;
import com.winning.vsts.merge.service.domain.VstsProject;
import com.winning.vsts.merge.service.service.VstsClient;
import com.winning.vsts.merge.service.service.VstsProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Set;

/**
 * 分支比较工作项
 *
 * @author xcl <xcl@winning.com.cn>
 * @date 2022/1/4
 */
@EnableJpaRepositories(basePackages = "com.winning.vsts.merge.service.jpa")
@SpringBootApplication(scanBasePackages = "com.winning.vsts.merge.service")
public class CompareBranchCommit implements ApplicationRunner {

    private final static Logger log = LoggerFactory.getLogger(AutoMergeApplication.class);

    @Autowired
    private VstsProjectService vstsProjectService;

    @Autowired
    private VstsClient vstsClient;

    @Autowired
    private VstsContext vstsContext;

    public static void main(String[] args) {
        new SpringApplicationBuilder()
            .main(CompareBranchCommit.class)
            .sources(CompareBranchCommit.class)
            .web(WebApplicationType.NONE)
            .build().run(args);
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        String source = "develop";
        String target = "RC";
        List<VstsProject> vstsProjects = vstsProjectService.queryVstsProject("winning-mds-knowledge",null);
        for (VstsProject vstsProject : vstsProjects) {
            RestTemplate restTemplate = vstsContext.restTemplate(vstsProject);
            List<JSONObject> jsonObjects = vstsClient.compareCommitIds(restTemplate, vstsProject, target, source);
            System.out.println(jsonObjects);
            Set<String> strings = vstsClient.compareItemIds(restTemplate, vstsProject, target, source);
            System.out.println(strings);
        }
    }
}
